Ext.define('mvcCouch.view.supplier.List' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.supplierlist',
    store: 'Suppliers',
    title : '供应商列表',
    initComponent: function() {
        this.columns = [
            {header: '公司名称', dataIndex: 'companyname', flex: 1},
            {header: '公司地址', dataIndex: 'companyaddress', flex: 1},
            {header: '邮政编码', dataIndex: 'postcode', flex: 1},      
            {header: '公司网站', dataIndex: 'website', flex: 1},            
            {header: '公司介绍', dataIndex: 'introduction', flex: 1},
            {header: '主营产品', dataIndex: 'mainproduct', flex: 1},
        ];
 		Ext.apply(this, {
            iconCls: 'icon-grid',
            frame: true,
            dockedItems: [{
                xtype: 'toolbar',
                items: [{
                    iconCls: 'icon-add',
                    text: '新增',
                    tooltip: '新增供应商',
                    scope: this,
                    handler: this.onAddClick
                }, {
                    iconCls: 'icon-delete',
                    text: '删除',
                    tooltip: '删除当前选中供应商',
                    disabled: true,
                    itemId: 'delete',
                    scope: this,
                    handler: this.onDeleteClick
                }, {
                    iconCls: 'icon-refresh',
                    text: '刷新',
                    tooltip: '如果您使用的是Safari浏览器,请在每次CRUD操作后刷新数据.',
                    itemId: 'load',
                    scope: this,
                    handler: this.onLoadClick
                }]
            },{
					xtype: "pagingtoolbar",
					store: this.store,
					dock: "bottom",
					displayInfo: true
			}]
        });
        this.height = 400;
        this.callParent(arguments);
		this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
    },
	onSelectChange: function(selModel, selections){
        this.down('#delete').setDisabled(selections.length === 0);
    },
    onLoadClick: function(){
        this.store.load();
    },
	onDeleteClick: function(){
        Ext.Msg.show({
             title:'确定删除吗？',
             msg: '如果删除此条供应商信息将删除其所有的产品信息.<br />你确定删除吗？',
             buttons: Ext.Msg.YESNO,
             fn: processResult,
             icon: Ext.MessageBox.WARNING,
             scope: this
        });
        function processResult(buttonId){
            if (buttonId == 'yes'){
                  var selection = this.getView().getSelectionModel().getSelection()[0];
                  if (selection) {
                      var rev = selection.get('_rev');
                      //console.log('rev id = ' + rev);
                      Ext.apply(this.store.proxy.extraParams, { 'rev': rev });
                      this.store.remove(selection);
                  };
                  this.store.sync();
                  Ext.destroyMembers(this.store.proxy.extraParams, 'rev');
                  alert('删除成功');
                  this.store.load();
            } 
        };
    },
    onAddClick: function(){
		var rnd="";
		for(var i=0;i<13;i++)
		rnd+=Math.floor(Math.random()*10);

		var params = new Object();
		params["companyname"] = "";
		params["companyaddress"] = "";
		params["postcode"] = "";
		params["website"] = "";
		params["introduction"] = "";
		params["mainproduct"] = "";
		params["doctype"] = "supplier";
		$.ajax({
			url: '/editdemo?_dc=' + rnd,
			type: 'POST',
			contentType: "application/json",
            dataType: 'json',
            data: JSON.stringify(params),
			success:function(data){				
		        alert('一个新的供应商已经被添加');
			}
		});
		
		this.store.load();
    }
});